<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/27
  Time: 19:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <title>上师管理模块</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js" type="text/javascript"></script>
</head>
<body>
<!--添加表单-->
<form class="layui-form" id="addForm" lay-filter="addFormFilter" style="display: none;padding: 15px 15px 5px 5px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">上师姓名</label>
        <div class="layui-input-block">
            <input type="hidden" name="guruImage"/>
            <input type="text" name="guruName" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上师头像</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="addImg">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">上师昵称</label>
        <div class="layui-input-block">
            <input type="text" name="guruNickname" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上师状态</label>
        <div class="layui-input-block">
            <input type="radio" name="guruStatus" value="0" title="正常">
            <input type="radio" name="guruStatus" value="1" title="冻结" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addFilter">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--添加按钮-->
<button class="layui-btn" id="add">添加</button>
<!--搜索框-->
<div class="layui-inline">
    <div class="layui-input-inline">
        <input type="text" class="layui-input" name="guruNickname" id="search" placeholder="请填写上师昵称"/>
    </div>
</div>
<button class="layui-btn layui-btn-sm" id="searchBtn">搜索</button><br/><br/>
<!--文件批量上传下载按钮   这两个按钮需要超级管理员才能使用  所以添加shiro标签库在此使用标签式授权-->
<shiro:hasRole name="superadmin">
    <div class="layui-inline">
        <button class="layui-btn layui-btn-sm" id="poi1">批量上传</button>
        <button class="layui-btn layui-btn-sm" id="poi2">批量下载</button>
    </div>
</shiro:hasRole>
<!--文件批量上传表单-->
    <div id="uploadDiv" class="layui-form-item" style="display: none">
        <label class="layui-form-label">批量上传</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="importFile">
                <i class="layui-icon">&#xe67c;</i>上传文件
            </button>
        </div>
    </div>
<!--展示表格-->
<table id="myTable" lay-filter="tableFilter"></table>
<!--表格显示图片-->
<script type="text/html" id="showImg">
    <img src="${pageContext.request.contextPath}/guru/{{d.guruImage}}"  height="100" width="200"/>
</script>
<!--toolbar组件-->
<script type="text/html" id="bar">
    <div class="layui-btn-container">
        <!--更新上师也采用标签式授权实现-->
        <shiro:hasPermission name="guru:update">
             <button class="layui-btn layui-btn-sm" lay-event="update">冻结/解冻</button>
        </shiro:hasPermission>
    </div>
</script>
<!--layui代码-->
<script>
    layui.use(['table','form','upload','layer'],function () {
        var table = layui.table;
        var form = layui.form;
        var upload = layui.upload;
        var layer = layui.layer;
        var $ = layui.$;
        //1、渲染表格
        var tableIns = table.render({
            elem:'#myTable',
            url:'${pageContext.request.contextPath}/cmfzGuru/show',
            page:true,
            limit:3,
            limits:[3,4,5],
            cols:[[
                {field:'guruId',title:'编号',sort:true,width:120},
                {field:'guruName',title:'上师姓名',sort:true,width:120},
                {field:'guruImage',title:'上师头像',templet:'#showImg'},
                // {field:'guruImage',title:'上师头像',sort:true,width:120},
                {field:'guruNickname',title:'上师昵称',sort:true,width:120},
                {field:'guruStatus',title:'上师状态',templet:function (g) {
                        if(g.guruStatus==0){
                            return '正常'
                        }else {
                            return '冻结'
                        }
                    }},
                {title:'操作',toolbar:'#bar'}
            ]]
        })
        //2、搜索操作
        $('#searchBtn').click(function () {
            tableIns.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    guruNickname: $('#search').val()
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        })
        //3、添加操作
        $('#add').click(function () {
            layer.open({
                type:1,
                title:'添加页面',
                area:'900px',
                content:$('#addForm')
            })
        })
        upload.render({
            elem:'#addImg',
            url: '${pageContext.request.contextPath}/cmfzGuru/upload',
            done:function (result) {
                if(result.flag){
                    layer.msg('上传成功');
                    form.val('addFormFilter',{//上传成功 回显上传后的文件名到表单隐藏起来
                        bannerImageUrl:result.fileName
                    })
                }else {
                    layer.msg('上传失败')
                }
            }
        })
        form.on('submit(addFilter)',function () {
            $.ajax({
                url:'${pageContext.request.contextPath}/cmfzGuru/add',
                data:$('#addForm').serialize(),
                success:function (result) {
                    if(result.flag){
                        layer.msg('添加成功');
                        layer.closeAll();
                        $('#addForm')[0].reset();
                        tableIns.reload();
                    }else {
                        layer.msg('添加失败');
                    }
                }
            })
            return false;
        })
        //4、toolbar组件的监听操作  修改上师状态
        table.on('tool(tableFilter)',function (obj) {
            console.log(obj)
            console.log(obj.data)
            console.log(obj.data.guruId)
            console.log(obj.tr)
            console.log(obj.event=='update')
            if(obj.event=='update'){
                $.ajax({
                    url:'${pageContext.request.contextPath}/cmfzGuru/update',
                    data:'guruId='+obj.data.guruId,
                    success:function (result) {
                        if(result.flag){
                            layer.msg('修改成功');
                            tableIns.reload();
                        }else{
                            layer.msg('修改失败')
                        }
                    }
                })
            }
        })
        //5、批量上传
        $('#poi1').click(function () {
            //点击批量上传按钮  打开上传页面
            layer.open({
                type: 1,
                title:'批量上传',
                area: '500px',
                content: $('#uploadDiv')
            })
            //上传按钮触发上传方法  文件上传组件
            upload.render({
                elem:'#importFile',
                url:'${pageContext.request.contextPath}/cmfzGuru/importFile',
                accept:'file',//注意  上传文件和上传图片类型 的上传接口设置不一样
                exts:'xls|xlsx',//上传文件的后缀名
                done:function (result) {
                    if(result.flag){
                        layer.msg('批量上传成功');
                        layer.closeAll();
                        tableIns.reload();
                    }else {
                        layer.msg('批量上传失败');
                    }
                }
            })
        })
        //5、批量下载
        $('#poi2').click(function () {
            /*$.ajax({
                url:'${pageContext.request.contextPath}/cmfzGuru/downLoads',
                success:function (result) {
                    if(result.flag){
                        alert('批量下载成功');
                    }else {
                        alert('批量下载失败');
                    }
                }
            })*/
            window.location.href='${pageContext.request.contextPath}/cmfzGuru/downLoads'
        })
    })
</script>
</body>
</html>
